Imports System.IO
Imports System.Data.SqlClient
Imports System
Imports System.Text
Imports System.Data.Odbc
Public Class FrmEvent
    Public GV_ISEDIT As Boolean = False


    Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxLaterality.SelectedIndexChanged

    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxCaseType.SelectedIndexChanged

    End Sub
    Private Sub Panel2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel2.Paint

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
        FrmDiagnosis.Close()
        FrmDiagnosis.Show()
    End Sub

    Private Sub FrmEvent_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Panel2.Enabled = False
        DOB.Value = Now
        DateTimePicker1.Value = Now
        LoadDoctor()
        LoadCaseType()
        LoadLaterality()
        LoadMetasynchronous()

        If GV_SELECTED_EVENTID <> "" Then
            ListEvent(GV_SELECTED_EVENTID)
            If GV_FRMEVENT_ALLOW_EDIT = True Then btnEdit_Click(sender, e)
        End If
        GV_FRMEVENT_ALLOW_EDIT = True

        Panel1.Enabled = GV_FRMEVENT_ALLOW_EDIT

        If Approved() = True Then
            'BtnAdd.Enabled = False
            'btnEdit.Enabled = False
            'btnDel.Enabled = False
            Panel1.Enabled = False
            Panel2.Enabled = False
        End If


    End Sub
    Function Approved() As Boolean
        Dim vStatus As String = ""
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("select * from Event where Event_id = '" & GV_SELECTED_EVENTID & "'", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                vStatus = IIf(IsDBNull(objReader("Status")), "", objReader("Status"))
            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
        If vStatus = "P" Then
            Approved = True
        Else
            Approved = False
        End If
    End Function

    Private Sub ListEvent(ByVal vEventID)
        Dim vEnteredDate, vUpdateDate, vLockDate As String

        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("select * from Event where event_id = '" & vEventID & "'", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                DOB.Value = IIf(IsDBNull(objReader("date_tb")), "01/01/1900", objReader("date_tb"))
                DateTimePicker1.Value = IIf(IsDBNull(objReader("date_cn")), "01/01/1900", objReader("date_cn"))
                cbxDoctor.Text = IIf(IsDBNull(objReader("doc_inc")), "", objReader("doc_inc"))
                cbxCaseType.Text = IIf(IsDBNull(objReader("case_type")), "", objReader("case_type"))
                cbxLaterality.Text = IIf(IsDBNull(objReader("brst_screen")), "", objReader("brst_screen"))
                CboxMeta.Text = IIf(IsDBNull(objReader("Metasyn_Status")), "NA", objReader("Metasyn_Status"))
                CboxMetasEvent.SelectedValue = IIf(IsDBNull(objReader("Metasynchronous")), "", objReader("Metasynchronous"))

                Lbl_EnBy.Text = "Entered By:" & vbCrLf & IIf(IsDBNull(objReader("EnteredBy")), "", objReader("EnteredBy"))
                Lbl_UpdBy.Text = "Updated By:" & vbCrLf & IIf(IsDBNull(objReader("UpdatedBy")), "", objReader("UpdatedBy"))
                Lbl_LockBy.Text = "Locked By:" & vbCrLf & IIf(IsDBNull(objReader("LockedBy")), "", objReader("LockedBy"))

                If IsDBNull(objReader("EnteredTime")) = True Then vEnteredDate = "" Else vEnteredDate = FormatDateTime(objReader("EnteredTime"), DateFormat.GeneralDate)
                If IsDBNull(objReader("UpdatedTime")) = True Then vUpdateDate = "" Else vUpdateDate = FormatDateTime(objReader("UpdatedTime"), DateFormat.GeneralDate)
                If IsDBNull(objReader("LockedTime")) = True Then vLockDate = "" Else vLockDate = FormatDateTime(objReader("LockedTime"), DateFormat.GeneralDate)

                Lbl_EnBy.Text = Lbl_EnBy.Text & vbCrLf & vEnteredDate
                Lbl_UpdBy.Text = Lbl_UpdBy.Text & vbCrLf & vUpdateDate
                Lbl_LockBy.Text = Lbl_LockBy.Text & vbCrLf & vLockDate


            Loop

        Catch e As Exception
            MsgBox(e.Source & e.Message)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()

    End Sub
    Private Sub LoadMetasynchronous()
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("spGetMetasyn", SqlConnection1)
        sqlCmd.Parameters.Add("@Pt_ID1", SqlDbType.Int)
        sqlCmd.Parameters.Add("@Event_Id", SqlDbType.Char)
        sqlCmd.Parameters("@Pt_ID1").Value = GV_CUR_PATID
        sqlCmd.Parameters("@Event_Id").Value = GV_SELECTED_EVENTID
        sqlCmd.CommandType = CommandType.StoredProcedure
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("brst_screen", GetType(String)))
            list.Columns.Add(New DataColumn("Event_ID", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("brst_screen")), "", objReader("brst_screen"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("Event_ID")), "", objReader("Event_ID"))
                i = i + 1
            Loop
            Me.CboxMetasEvent.DataSource = list
            Me.CboxMetasEvent.DisplayMember = "brst_screen"
            Me.CboxMetasEvent.ValueMember = "Event_ID"

        Catch e As Exception
            MsgBox(e.Source & e.Message)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub

    Private Sub LoadDoctor()


        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value  83", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("code_desc", GetType(String)))
            list.Columns.Add(New DataColumn("code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("code_desc")), "", objReader("code_desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("code")), "", objReader("code"))
                i = i + 1
            Loop
            Me.cbxDoctor.DataSource = list
            Me.cbxDoctor.DisplayMember = "code_desc"
            Me.cbxDoctor.ValueMember = "code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub LoadCaseType()


        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value 84", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("code_desc", GetType(String)))
            list.Columns.Add(New DataColumn("code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("code_desc")), "", objReader("code_desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("code")), "", objReader("code"))
                i = i + 1
            Loop
            Me.cbxCaseType.DataSource = list
            Me.cbxCaseType.DisplayMember = "code_desc"
            Me.cbxCaseType.ValueMember = "code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub LoadLaterality()


        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value 9", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("code_desc", GetType(String)))
            list.Columns.Add(New DataColumn("code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("code_desc")), "", objReader("code_desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("code")), "", objReader("code"))
                i = i + 1
            Loop
            Me.cbxLaterality.DataSource = list
            Me.cbxLaterality.DisplayMember = "code_desc"
            Me.cbxLaterality.ValueMember = "code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        GV_ISEDIT = False
        Panel2.Enabled = True
        btnEdit.Visible = False
        btnDel.Visible = False
        BtnAdd.Visible = False
        btnSave.Visible = True
        btnCancel.Visible = True
        ClearText()

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'Promp_EventMsg()
        Try
            SavePatientEvent()
            'Panel2.Enabled = False
            Close()
            FrmDiagnosis.Close()
            FrmDiagnosis.Show()
            FrmDiagnosis.Focus()
        Catch ex As Exception
            ErrMsg.Text = "Pls fill in all fields"
            ErrMsg.Visible = True
        End Try

       
    End Sub
    Private Sub Promp_EventMsg()
        Try
            Dim vMsg As String
            Dim SqlConnection1 As New SqlConnection()
            SqlConnection1 = New SqlConnection(GV_CONNETION)
            If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
            Dim sqlCmd = New SqlCommand("spCheckB2BMsg", SqlConnection1)
            sqlCmd.Parameters.Add("@BTB_DT", SqlDbType.DateTime)
            sqlCmd.Parameters.Add("@CONSUL_DT", SqlDbType.DateTime)
            sqlCmd.Parameters("@BTB_DT").Value = DOB.Value
            sqlCmd.Parameters("@CONSUL_DT").Value = DateTimePicker1.Value
            sqlCmd.CommandType = CommandType.StoredProcedure
            Dim rsReader As SqlDataReader
            rsReader = sqlCmd.ExecuteReader()
            If rsReader.Read() Then
                vMsg = IIf(IsDBNull(rsReader("MSG")), "", rsReader("MSG"))
            End If
            rsReader.Close()
            SqlConnection1.Close()

            If Trim(vMsg) <> "" Then
                Dim response As Integer
                response = MsgBox(vMsg, MsgBoxStyle.YesNo, "Event")
                If response = 7 Then
                    Exit Sub
                End If
            End If

            SavePatientEvent()
            Panel2.Enabled = False
            Close()
            FrmDiagnosis.Close()
            FrmDiagnosis.Show()
            FrmDiagnosis.Focus()

        Catch ex As Exception
            Dim vStrErr As String
            vStrErr = ex.Message & " ||Promp_EventMsg||"
            Write_ErrLog(vStrErr)

            Exit Sub
        End Try
    End Sub
    Private Sub Write_ErrLog(ByVal vStrErr)
        'Write error log into errlog.txt
        Dim strAppPath
        strAppPath = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\") - 1)

        Dim myStreamWriter As New System.IO.StreamWriter(strAppPath & "\Errlog.txt", True)
        myStreamWriter.WriteLine(Now & "  " & vStrErr)
        myStreamWriter.Flush()
        myStreamWriter.Close()
    End Sub
    Private Sub SavePatientEvent()
        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("spInsertEvent", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()

        myCommand.Parameters.Add("@Event_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Pt_id1", SqlDbType.VarChar)
        myCommand.Parameters.Add("@date_tb", SqlDbType.DateTime)
        myCommand.Parameters.Add("@date_cn", SqlDbType.DateTime)
        myCommand.Parameters.Add("@doc_inc", SqlDbType.VarChar)
        myCommand.Parameters.Add("@case_type", SqlDbType.VarChar)
        myCommand.Parameters.Add("@brst_screen", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Metasyn_Status", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Metasynchronous", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Event_Id_old", SqlDbType.VarChar)

        If GV_ISEDIT = True Then
            myCommand.Parameters("@Event_ID").Value = GV_SELECTED_EVENTID
        Else
            myCommand.Parameters("@Event_ID").Value = "0"
        End If
        myCommand.Parameters("@Pt_id1").Value = GV_CUR_PATID
        myCommand.Parameters("@date_tb").Value = FormatDateTime(DOB.Value, DateFormat.ShortDate)
        myCommand.Parameters("@date_cn").Value = FormatDateTime(DateTimePicker1.Value, DateFormat.ShortDate)
        myCommand.Parameters("@doc_inc").Value = Trim(cbxDoctor.Text)
        myCommand.Parameters("@case_type").Value = Trim(cbxCaseType.Text)
        myCommand.Parameters("@brst_screen").Value = Trim(cbxLaterality.Text)
        myCommand.Parameters("@Metasyn_Status").Value = Trim(CboxMeta.Text)
        myCommand.Parameters("@Metasynchronous").Value = Trim(CboxMetasEvent.SelectedValue)
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.Parameters("@Event_Id_old").Value = CboxMetasEvent.SelectedValue
        myCommand.ExecuteNonQuery()
        
        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If GV_SELECTED_EVENTID <> "" Then
            GV_ISEDIT = True
            Panel2.Enabled = True
            btnEdit.Visible = False
            btnDel.Visible = False
            BtnAdd.Visible = False
            btnSave.Visible = True
            btnCancel.Visible = True
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        If GV_ISEDIT = False Then ClearText() Else ListEvent(GV_SELECTED_EVENTID)
        Panel2.Enabled = False
        BtnAdd.Visible = True
        btnEdit.Visible = True
        btnDel.Visible = True
        btnSave.Visible = False
        btnCancel.Visible = False

    End Sub
    Private Sub ClearText()
        DOB.Value = Now
        DateTimePicker1.Value = Now
        cbxDoctor.SelectedIndex = -1
        cbxCaseType.SelectedIndex = -1
        cbxLaterality.SelectedIndex = -1
        Lbl_EnBy.Text = "Entered By:"
        Lbl_UpdBy.Text = "Updated By:"
        Lbl_LockBy.Text = "Locked By:"
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If GV_SELECTED_EVENTID <> "" Then
            Dim response As Integer

            response = MsgBox("Are you sure want to delete this record?", MsgBoxStyle.YesNo, "Delete")
            If response = 7 Then
                Exit Sub
            End If

            Dim myCommand As SqlCommand
            Dim SqlConn As New SqlConnection()
            SqlConn = New SqlConnection(GV_CONNETION)
            myCommand = New SqlCommand("spDelEvent", SqlConn)
            myCommand.CommandType = CommandType.StoredProcedure
            SqlConn.Open()

            myCommand.Parameters.Add("@Event_ID", SqlDbType.VarChar)
            myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
            myCommand.Parameters("@Event_ID").Value = GV_SELECTED_EVENTID
            myCommand.Parameters("@EnteredBy").Value = GV_USER
            myCommand.ExecuteNonQuery()

            myCommand.Cancel()
            SqlConn.Close()
            myCommand = Nothing
            SqlConn = Nothing
            Close()
        End If
    End Sub

    Private Sub BtnLock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLock.Click
        If BtnLock.Text = "Lock" Then
            If GV_FRMEVENT_ALLOW_APPROVE = False Then
                MsgBox("Permission denied.", MsgBoxStyle.Information)
                Exit Sub
            End If
        End If

        If BtnLock.Text = "UnLock" Then
            If GV_FRMEVENT_ALLOW_APPROVE = False Then
                MsgBox("Permission denied.", MsgBoxStyle.Information)
                Exit Sub
            End If
        End If

        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("sp_LockUnlockEvent", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()
        myCommand.Parameters.Add("@EVENT_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
        myCommand.Parameters.Add("@LockFlag", SqlDbType.VarChar)

        myCommand.Parameters("@EVENT_ID").Value = GV_SELECTED_EVENTID
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.Parameters("@LockFlag").Value = Trim(BtnLock.Text)
        myCommand.ExecuteNonQuery()

        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing

        If BtnLock.Text = "Lock" Then
            BtnLock.Text = "UnLock"
            btnEdit.Enabled = False
            btnSave.Enabled = False
            btnDel.Enabled = False
            Lbl_LockBy.Text = "Locked By:" & vbCrLf & GV_USERNAME & vbCrLf & FormatDateTime(Now, DateFormat.GeneralDate)
        Else
            BtnLock.Text = "Lock"
            btnEdit.Enabled = GV_FRMEVENT_ALLOW_EDIT
            btnSave.Enabled = GV_FRMEVENT_ALLOW_EDIT
            btnDel.Enabled = GV_FRMEVENT_ALLOW_EDIT
            Lbl_LockBy.Text = "Locked By:"
        End If

    End Sub

    Private Sub CboxMeta_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboxMeta.SelectedIndexChanged
        If CboxMeta.Text = "Yes" Then
            CboxMetasEvent.Enabled = True
        Else
            CboxMetasEvent.Enabled = False
            CboxMetasEvent.SelectedValue = ""
        End If
    End Sub

    
    
End Class